package com.mjh.zhujianju.service.supervise.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.utils.XTListUtils;
import com.mjh.zhujianju.dao.supervise.SupervisionEstimateMapper;
import com.mjh.zhujianju.enums.SupervisionEstimateEnum;
import com.mjh.zhujianju.enums.SupervisionTypeEnum;
import com.mjh.zhujianju.model.supervise.SupervisionEstimateEO;
import com.mjh.zhujianju.service.supervise.SupervisionEstimateService;
import com.mjh.zhujianju.vo.supervision.StatisticsVO;
import com.mjh.zhujianju.vo.supervision.SupervisionEstimateVO;

@Service
public class SupervisionEstimateServiceImpl extends BaseServiceImpl<SupervisionEstimateEO>
		implements SupervisionEstimateService {

	@Resource
	private SupervisionEstimateMapper supervisionEstimateMapper;

	public SupervisionEstimateServiceImpl(SupervisionEstimateMapper supervisionEstimateMapper) {
		super(supervisionEstimateMapper);
	}

	@Override
	public void getQualitys(StatisticsVO vo) {
		Integer internal_satisfaction = 0; // 内部- 满意
		Integer internal_basic = 0; // 内部- 基本满意
		Integer internal_no = 0; // 内部- 不满意
		// 内部工作的
		vo.setType(SupervisionTypeEnum.internal.getCode());
		List<SupervisionEstimateEO> internalList = supervisionEstimateMapper.selectQualitys(vo);
		if (!XTListUtils.isEmpty(internalList)) {
			for (SupervisionEstimateEO supervisionEstimateEO : internalList) {
				if (SupervisionEstimateEnum.satisfaction.getCode().equals(supervisionEstimateEO.getEstimate())) {
					internal_satisfaction += 1;
				} else if (SupervisionEstimateEnum.basic.getCode().equals(supervisionEstimateEO.getEstimate())) {
					internal_basic += 1;
				} else if (SupervisionEstimateEnum.no.getCode().equals(supervisionEstimateEO.getEstimate())) {
					internal_no += 1;
				}
			}
			vo.setInternal_basic(internal_basic);
			vo.setInternal_no(internal_no);
			vo.setInternal_satisfaction(internal_satisfaction);
		}
		// 信访工作的 
		vo.setType(SupervisionTypeEnum.visit.getCode());
		List<SupervisionEstimateEO> visitList = supervisionEstimateMapper.selectQualitys(vo);
		if (!XTListUtils.isEmpty(visitList)) {
			internal_satisfaction = 0; // 内部- 满意
			internal_basic = 0; // 内部- 基本满意
			internal_no = 0; // 内部- 不满意
			for (SupervisionEstimateEO supervisionEstimateEO : visitList) {
				if (SupervisionEstimateEnum.satisfaction.getCode().equals(supervisionEstimateEO.getEstimate())) {
					internal_satisfaction += 1;
				} else if (SupervisionEstimateEnum.basic.getCode().equals(supervisionEstimateEO.getEstimate())) {
					internal_basic += 1;
				} else if (SupervisionEstimateEnum.no.getCode().equals(supervisionEstimateEO.getEstimate())) {
					internal_no += 1;
				}
			}
			vo.setVisit_basic(internal_basic);
			vo.setVisit_no(internal_no);
			vo.setVisit_satisfaction(internal_satisfaction);
		}
	}

	@Override
	public Map<Long, SupervisionEstimateEO> getMap(List<Long> supervisionIds) {
		List<SupervisionEstimateEO> list = supervisionEstimateMapper.selectMapBySupervisionId(supervisionIds);
		if (XTListUtils.isEmpty(list)) {
			return null;
		}
		Map<Long, SupervisionEstimateEO> map = new HashMap<>();
		for (SupervisionEstimateEO supervisionEstimateEO : list) {
			map.put(supervisionEstimateEO.getSupervisionId(), supervisionEstimateEO);
		}
		return map;
	}

	@Override
	public SupervisionEstimateVO queryLetterEvaluateDetail(Long id, Long userId) {
		SupervisionEstimateVO supervisionEstimateVO = null;
		SupervisionEstimateEO supervisionEstimateEO = new SupervisionEstimateEO();
		supervisionEstimateEO.setSupervisionId(id);
		supervisionEstimateEO.setUserId(userId);
		List<SupervisionEstimateEO> supervisionEstimateEOS = supervisionEstimateMapper
				.selectListByCondition(supervisionEstimateEO);
		if (!CollectionUtils.isEmpty(supervisionEstimateEOS)) {
			supervisionEstimateVO = new SupervisionEstimateVO();
			SupervisionEstimateEO supervisionEstimateEO1 = supervisionEstimateEOS.get(0);
			BeanUtils.copyProperties(supervisionEstimateEO1, supervisionEstimateVO);
			supervisionEstimateVO.setEstimateName(SupervisionEstimateEnum.getName(supervisionEstimateVO.getEstimate()));
			return supervisionEstimateVO;
		}

		return null;
	}

}
